package com.ljw.store.dao;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.ljw.store.entity.Store;


@Component
@Transactional
public class StoreDaoImp implements StoreDao{

	private SessionFactory sessionFactory;
	
	@Resource
	private HibernateTemplate hibernateTemplate = null;
	private Query query;
	@Resource
	public void setSessionFactory(SessionFactory sessionFactory){
		this.sessionFactory = sessionFactory;
	}
	
	public void saveStore(Store store) {
		this.hibernateTemplate.save(store);
	}

	public void deleteStore(Store store) {
		Session session = sessionFactory.getCurrentSession();
		session.delete(store);
	}

	public Store updateStore(Store store) {
		sessionFactory.getCurrentSession().merge(store);
		return store;
	}

	public List<Store> searchStore(Store store) {
		// TODO Auto-generated method stub
		query = sessionFactory.getCurrentSession().createQuery(
				"from Store where shopId=?");
		query.setInteger(0, store.getShopId());
		List<Store> storeList = query.list();
		System.out.println(storeList.size());
		return storeList;
	}

	public List<Store> searchAllStore(int pageNumber , int pageSize) {
		org.hibernate.classic.Session session = sessionFactory.getCurrentSession();
		List<Store> itemStore = new ArrayList<Store>();
		String hql = "from Store";
		query = session.createQuery(hql);
		query.setFirstResult((pageNumber - 1)*pageSize);
		query.setMaxResults(pageSize);
		itemStore = query.list();
		return itemStore;
	}

	public int storeAmount() {
		int storeAmount = 0;
		org.hibernate.classic.Session session = sessionFactory.getCurrentSession();
		String hql = "select count(*) from Store";
		query = session.createQuery(hql);
		long  l = (Long)query.uniqueResult();
		storeAmount = (int)l;
		return storeAmount;
	}
	
	public int[] pageNumber(int storeAmount, String pageNumberStr, int pageSize) {
		// TODO Auto-generated method stub
		int[] paging = new int[2];
		int pageNumber = 1;
		pageNumber = Integer.parseInt(pageNumberStr);
		if(pageNumber <= 0){
			pageNumber = 1;
		}
		int totalPage = 1;
		if(storeAmount > 0){
			totalPage = storeAmount%pageSize==0?(storeAmount/pageSize):(storeAmount/pageSize+1);
		}
		if(pageNumber > totalPage){
			pageNumber = totalPage;
		}
		paging[0] = pageNumber;
		paging[1] = totalPage;
		return paging;
	}
	public List<Store> HotsearchAllStore(int pageNumber, int pageSize) {
		// TODO Auto-generated method stub
		org.hibernate.classic.Session session = sessionFactory.getCurrentSession();
		List<Store> itemStore = new ArrayList<Store>();
		String hql = "from Store order by number desc";
		query = session.createQuery(hql);
		query.setFirstResult((pageNumber - 1)*pageSize);
		query.setMaxResults(pageSize);
		itemStore = query.list();
		return itemStore;
	}

	public Store searchspStore(String string) {
		// TODO Auto-generated method stub
		query = sessionFactory.getCurrentSession().createQuery(string);
		List<Store> list= query.list();	
		return list.get(0);
	}

	public void deleteStoreHQL(String string) {
		// TODO Auto-generated method stub
		sessionFactory.getCurrentSession().createQuery(string).executeUpdate();

	}
}
